<?php

/*
Copyright 2005-2011 Textpattern CMS Project
All Rights Reserved.

This file is part of Textpattern.

Textpattern is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

if (!defined('txp'))
{
	header('HTTP/1.1 403 Forbidden');
	exit('<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You don\'t have permission to access the requested resource on this server.</p></body></html>');
}

//------------------------------------------------------------------------------

class _UsersModel extends SparkModel
{
	private $columns;
	
	//---------------------------------------------------------------------------

	public function __construct($params)
	{
		parent::__construct($params);
		$this->columns = array_flip(array('user_id', 'name', 'pass', 'RealName', 'email', 'privs', 'last_access', 'nonce',));
	}

	//---------------------------------------------------------------------------
	
	public function addUser(&$user)
	{
		$db = $this->loadDB();
		
		unset($user['user_id']);
		$this->filterUser($user);
		$this->validateUser($user);

		$db->insertRow('txp_users', $user);
		$user['user_id'] = $db->lastInsertID();
	}

	//---------------------------------------------------------------------------
	
	public function updateUser($user)
	{
		$db = $this->loadDB();

		$id = $user['user_id'];
		unset($user['user_id']);
		$this->filterUser($user);

		$db->updateRows('txp_users', $user, 'user_id=?', $id);
	}

	//---------------------------------------------------------------------------
	
	public function deleteUser($user)
	{
		$db = $this->loadDB();

		$db->deleteRows('txp_users', 'user_id=?', $user['user_id']);
	}

	//---------------------------------------------------------------------------
	
	public function getUserByID($id, $columns = '*')
	{
		$db = $this->loadDB();

		if (!$row = $db->selectRow('txp_users', $columns, 'user_id=?', $id))
		{
			return false;
		}
		
		return $row;
	}

	//---------------------------------------------------------------------------
	
	public function findUsers($filters, $columns = '*')
	{
		$db = $this->loadDB();

		$where = $db->buildConjunction('txp_users', $filters, $bind);

		return $db->selectRows('txp_users', $columns, $where, $bind);
	}

	//---------------------------------------------------------------------------
	
	private function filterUser(&$user)
	{
		$user = array_intersect_key($user, $this->columns);
	}

	//---------------------------------------------------------------------------
	
	private function validateUser(&$user)
	{
		if (empty($user['name']))
		{
			throw new SparkHTTPException_BadRequest();
		}
	}

//------------------------------------------------------------------------------
	
}
